package br.com.orion.sga.controller;

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;

import br.com.orion.sga.model.entities.Funcionario;

@ManagedBean(name="sessionController")
@SessionScoped
public class SessionController implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Funcionario funcionario;
	
	@PostConstruct
	public void init() {
		if (funcionario == null)
			funcionario = new Funcionario();
	}
	
	public SessionController() {
        funcionario = new Funcionario();
        SecurityContext context = SecurityContextHolder.getContext();
        if (context instanceof SecurityContext){
            Authentication authentication = context.getAuthentication();
            if (authentication instanceof Authentication){
                funcionario.setUsername(((Funcionario)authentication.getPrincipal()).getUsername());
            }
        }
    }
	
	
	public Funcionario getFuncionario() {
		return funcionario;
	}
	
	public void setFuncionario(Funcionario funcionario) {
		this.funcionario = funcionario;
	}
}
